/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月11日         Root.Lu
 */

package com.coocaa.x.framework.utils;

public class Size2String {
    private static final double K = 1024;

    public static String byte2KB(long size) {
        return String.format("%.2f", (double) size / K) + "KB";
    }

    public static String byte2MB(long size) {
        return String.format("%.2f", (double) size / Math.pow(K, 2)) + "MB";
    }

    public static String byte2GB(long size) {
        return String.format("%.2f", (double) size / Math.pow(K, 3)) + "GB";
    }

    public static String byte2Auto(long size) {
        int pow = 0;
        while (true) {
            if ((size / (long) Math.pow(K, pow)) < 1)
                break;
            pow++;
        }
        switch (pow) {
            case 0:
            case 1:
                return size + "B";
            case 2:
                return byte2KB(size);
            case 3:
                return byte2MB(size);
            case 4:
                return byte2GB(size);
            default:
                return "TMD so big!!";
        }
    }
}
